package com.allwees.data.analysis.stat.entity;

import com.allwees.bs.core.model.stat.EStatType;
import com.allwees.bs.core.modelbase.constant.EPlatform;
import com.allwees.bs.core.modelbase.entity.BaseEntity;
import com.allwees.core.common.util.encrypt.MD5Util;
import com.allwees.data.analysis.stat.EStatMode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math.util.MathUtils;

import javax.persistence.Column;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

/**
 *
 *
 * @author yangylsky
 * @since 2020-04-16
 *
 */
@Data
@EqualsAndHashCode(callSuper=false)
@ToString
@Accessors(chain = true)
public abstract class BaseStatEntity extends BaseEntity {
    private EStatType statType;

    private String statTypeDesc;

    protected EPlatform platform;

	@Column(length = 64)
    protected String name;

	@Column(length = 64)
    protected String bsKey;

    @Temporal(TemporalType.DATE)
    protected Date date;

    protected Double value;

    protected abstract EStatMode statMode();

    public BaseStatEntity setValue(Double value) {
    	this.value = MathUtils.round(value, 4);
    	return this;
    }

    public String uniqueId() {
        String s = statMode()
                + (statType + "|")
        		+ (platform == null ? "" : platform + "|")
                + (StringUtils.isBlank(bsKey) ? "" : bsKey + "|")
                + (date.getTime());
        return MD5Util.digest(s);
    }
}
